package org.jsping.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsping.entity.Music;
import org.jsping.service.impl.MusicServiceImpl;

import com.alibaba.fastjson.JSON;

/**
 * 音乐控制模块
 * 1.根据id来请求具体的音乐信息 2.根据路径来请求具体的音乐信息
 */
@WebServlet("/MusicControl.html")
public class MusicControl extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public MusicControl() {
        super();
    }

    MusicServiceImpl msi = new MusicServiceImpl();
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mid = request.getParameter("mid");
		String murl = request.getParameter("murl");
		Music music = null;
		//如果传过来的是mid
		if(mid!=null) {
			int myid = Integer.valueOf(mid); 
			music = msi.getMusicInfo(myid);       //根据id拿到音乐
		}
		//如果传过来的是歌曲路径
		if(murl!=null) {
			music = msi.getMusicInfoByURl(murl);  //根据路径拿到音乐
		}
		PrintWriter out = response.getWriter();
		String jsonStr = JSON.toJSONString(music);
		out.print(jsonStr);
		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
